package com.zhaojiadi.spring.bean.definition;

import com.zhaojiadi.thinking.in.spring.ioc.overview.domain.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.Import;

/**
 * 实例化Bean
 * {@link org.springframework.beans.factory.config.BeanDefinition} 注解示例
 */
public class BeanDefinitionCreationDemo {

    public static void main(String[] args) {
        //1. 通过BeanDefinition构建
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
        //通过属性设置
        beanDefinitionBuilder
                .addPropertyValue("id",1)
                .addPropertyReference("name","小马哥");
        //获取BeanDefinition实例
        AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
        //BeanDefinition并非Bean终态,可以自定义修改

        //2. 通过AbstractBeanDefinition以及派生类
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        //设置bean类型
        genericBeanDefinition.setBeanClass(User.class);
        //通过MutablePropertyValues 批量操作属性
        MutablePropertyValues propertyValues = new MutablePropertyValues();
        propertyValues.add("id",1).add("name","小马哥");
        //通过set MutablePropertyValues 批量操作属性
        genericBeanDefinition.setPropertyValues(propertyValues);


    }
}
